using RAG.Application.Dtos.SystemLog;
using RAG.Application.Services.Interfaces;
using RAG.Domain.Entities.App;
using RAG.Domain.Repositories;
using RAG.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace RAG.Application.Services.Implementations;

public class SystemLogService : ISystemLogService
{
    private readonly IRepository<SystemLogs> _systemLogRepository;
    private readonly RagAIDbContext _context;

    public SystemLogService(IRepository<SystemLogs> systemLogRepository, RagAIDbContext context)
    {
        _systemLogRepository = systemLogRepository;
        _context = context;
    }

    public async Task<SystemLogDto> CreateSystemLogAsync(SystemLogDto systemLogDto)
    {
        try
        {
            var systemLog = new SystemLogs
            {
                Id = Guid.NewGuid(),
                ActionType = systemLogDto.ActionType,
                Action = systemLogDto.Action,
                Target = systemLogDto.Target,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = systemLogDto.CreatedBy,
                UpdatedBy = systemLogDto.CreatedBy
            };

            var createdLog = await _systemLogRepository.CreatedAsync(systemLog);

            return new SystemLogDto
            {
                Id = createdLog.Id,
                ActionType = createdLog.ActionType,
                Action = createdLog.Action,
                Target = createdLog.Target,
                CreatedAt = createdLog.CreatedAt,
                CreatedBy = createdLog.CreatedBy
            };
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"创建系统日志失败: {ex.Message}", ex);
        }
    }

    public async Task<IEnumerable<SystemLogDto>> GetAllSystemLogsAsync()
    {
        try
        {
            var logs = await _context.SystemLogs
                .OrderByDescending(l => l.CreatedAt)
                .ToListAsync();

            return logs.Select(log => new SystemLogDto
            {
                Id = log.Id,
                ActionType = log.ActionType,
                Action = log.Action,
                Target = log.Target,
                CreatedAt = log.CreatedAt,
                CreatedBy = log.CreatedBy
            });
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"获取系统日志列表失败: {ex.Message}", ex);
        }
    }

    public async Task<IEnumerable<SystemLogDto>> GetSystemLogsByActionTypeAsync(string actionType)
    {
        try
        {
            var logs = await _context.SystemLogs
                .Where(l => l.ActionType.Contains(actionType))
                .OrderByDescending(l => l.CreatedAt)
                .ToListAsync();

            return logs.Select(log => new SystemLogDto
            {
                Id = log.Id,
                ActionType = log.ActionType,
                Action = log.Action,
                Target = log.Target,
                CreatedAt = log.CreatedAt,
                CreatedBy = log.CreatedBy
            });
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"根据操作类型查询系统日志失败: {ex.Message}", ex);
        }
    }
} 